const {creatOrUpdate,findCarts,updateCarts,removeCarts,selectedAllCarts,selectUnCarts} = require('../service/cart.service')
const {cartFormatError} = require('../constant/err.type')
class cart{

    async add(ctx){
        //将商品添加到购物车
        //1.解析goods_id,user_id
        const {goods_id} = ctx.request.body
        const user_id = ctx.state.user.id
        //2.操作数据库
        const res = await creatOrUpdate({goods_id,user_id})
        //3.返回结果
        ctx.body = {
            code:0,
            message:'添加成功',
            result:res
        }
        // next()
    }
    async findAll(ctx){
        //1.解析请求参数
        const {pageNum=1,pageSize=10} = ctx.request.query
        //2.操作数据库
        const res = await findCarts({pageNum,pageSize})
        //3.返回结果
        ctx.body = {
            code:0,
            message:'获取购物车列表成功',
            result:res
        }
    }
    async update(ctx,next){
        try {
            
            //1.解析参数
            const id = ctx.request.params.id
            const {number,selected} = ctx.request.body
            console.log(number,selected);
            
            if(number===undefined && selected===undefined){
                cartFormatError.result = 'number和selected不能同时为空'
                return ctx.app.emit('error',cartFormatError,ctx)
            }
    
            //2.操作数据库
            const res = await updateCarts({id,number,selected})
            // console.log(res);
            
            //3.返回数据
            if(!res) {
                cartFormatError.result = '没有找到要修改的数据'
                return ctx.app.emit('error',cartFormatError,ctx)
             }else{
                 ctx.body = {
                     code:0,
                     message:'更新数据库成功',
                     result:res
                 }
             }
    
            
        } catch (error) {
            console.log(error);
            
        }
    }
    async remove(ctx){
        const {ids} = ctx.request.body
        const res = await removeCarts(ids)
        
        if(res==0) {
            return ctx.body = {
                code:0,
                message:'删除失败',
                result:res
            }
        }else{
            return ctx.body={
                code:0,
                message:'删除成功',
                result:res
            }
        }
        
    }
    async selectAll(ctx){
        try {
        const user_id = ctx.state.user.id
        console.log(ctx.state.user);
        //如果穿过来的数字是1，那么就全选，0则全不选
        const {selecteId} = ctx.request.body
        const res = await selectedAllCarts({user_id,selecteId})
        
        return ctx.body ={
            code:0,
            message:`'购物车商品全部${selecteId ?'选中':'反选' }'`,
            result:res
        }   
        } catch (error) {
         console.log(error);
            
        }
        
    }
    async selectUnAll(ctx){
        const user_id = ctx.state.user.id
        
        const res = await selectUnCarts(user_id,selecteId)
        return ctx.body={
            code:0,
            message:(`'购物车商品全不选成功'`),
            result:res
        }
    }
}
module.exports = new cart()